const db = require('../../models/index');
const DAO = require('../../dao/sql-option-dao');
const { sendEmail } = require('../../utils/emailsend-util');
const Message = db.message;

// 创建留言，并通知
exports.create = (req, res) => {
    const createInfo = req.body;
    DAO.create(Message, createInfo, data => {
        let noticeInfo = {
            eMail: '1772590496@qq.com',
            html: `您收到一份来自 ${createInfo.eMail}，昵称为：${createInfo.nickname} 的留言：${ createInfo.content } ---快去查看。`
        };
        sendEmail(noticeInfo).then(info => {
            res.sendResult(data)
        })
    })
};

// 回复留言
exports.replyMsg = (req, res) => {
    const replyInfo = req.body;
    let mailInfo = {
        eMail: replyInfo.eMail,
        html: replyInfo.content
    };
    sendEmail(mailInfo).then(info => {
        Message.update({ status:true }, {
            where: { id: replyInfo.id }
        }).then(data => {
            res.status(200).sendResultAto(data, 200, '回复成功');
        })
    }).catch(error => {
        res.status(500).sendResultAto(error, 605, '回复失败');
    });
};

// 查询全部留言
exports.findAll = (req, res) => {
    const fAllInfo = req.body;
    DAO.list(Message, fAllInfo, list => {
        res.sendResult(list);
    })
};

// 根据id查询留言
exports.findOne = (req, res) => {
    const fOneInfo = req.body;
    DAO.findOne(Message, fOneInfo, data => {
        res.sendResult(data);
    })
};

// 更新留言
exports.update = (req, res) => {
    const updateInfo = req.body;
    DAO.update(Message, updateInfo, { id:updateInfo.id }, data => {
        res.sendResult(data);
    })
};

// 根据id 删除留言
exports.delete = (req, res) => {
    const deleteInfo = req.body;
    DAO.delete(Message, { id:deleteInfo.id }, data => {
        res.sendResult(data);
    })
};

// 删除全部留言
exports.deleteAll = (req, res) => {
    DAO.deleteAll(Message, data => {
        res.sendResult(data);
    });
};


